package com.example.good.utils;

import com.example.good.entity.PageVO;

import java.util.ArrayList;
import java.util.List;

public class PageUtils {
    
    public static <E> PageVO<E> page(List<E> list, int currentPageIndex, int pageSize){
        PageVO<E> page = new PageVO<E>(currentPageIndex, pageSize);
        if (list == null){
            return page;
        }
        if (list.size() == 0){
            return page;
        }
        
        //记录总的数据条数
        int count = list.size();
        page.setCount(count);
        
        //页面数
        int pageTotal = 0;
        if (count % pageSize == 0){
            pageTotal = count / pageSize;
        }else{
            pageTotal = count / pageSize + 1;
        }
        page.setPageTotal(pageTotal);
        
        //开始索引
        int startIndex = 0;
        
        int endIndex = 0;
        
        if (currentPageIndex != pageTotal){
            startIndex = (currentPageIndex - 1) * pageSize;
            endIndex = startIndex + pageSize;
        }else {
            startIndex = (currentPageIndex - 1) * pageSize;
            endIndex = count;
        }
        
        List pageList = new ArrayList();
        if (startIndex > list.size() || endIndex > list.size()){
            
        }else {
            pageList = list.subList(startIndex, endIndex);
        }
        page.setCurrentPageDateList(pageList);
        return page;
        
        
    }
}
 